package cheqex.update;

import javax.swing.*;
import javax.swing.event.*;

import cheqex.update.controllers.ActivitiesController;
import cheqex.update.controllers.DateController;
import cheqex.update.controllers.DepositController;

import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import java.sql.*;

public class DeleteDepositCheckView extends JFrame implements ActionListener{
	
	private JTextField prNumber;	
	private JButton delete;
	private JPanel jl;
	private int key;
	private DepositView depositView;
	
	public JPanel getDepositView() {
		return depositView;
	}

	public void setDepositView(DepositView depositView) {
		this.depositView = depositView;
	}
	
	public DeleteDepositCheckView(DepositView depositView){
		this.depositView = depositView;
		initialize(depositView);
	}
	
	//closes login frame
	private void closeframe(){
		this.setVisible(false);
	}
	
	public void initialize(DepositView depositView){
		prNumber = new JTextField(20);
		prNumber.addKeyListener(new KeyListener(){

			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER){
					deleteData();
				}
				
			}

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		});

		delete = new JButton("Delete");
		
		JLabel orNo = new JLabel("PR Number:");
		orNo.setLabelFor(prNumber);
	
		delete.setMnemonic(KeyEvent.VK_D);
		delete.setActionCommand("delete");
		delete.setEnabled(true);
		delete.addActionListener(this);
		
		JPanel jl = new JPanel();
		jl.add(orNo);
		jl.add(prNumber);
		jl.add(delete);
		jl.setVisible(true);
		
		this.add(jl);
		this.pack();
		this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		this.setTitle("Deleting");
	}
	
	private void deleteData(){
		
		boolean err = true;
		if(prNumber.getText().length()!=0){
			
			if(!DepositController.isNumeric(prNumber.getText())){
				err = false;
				JOptionPane.showMessageDialog(null,"PR number should only consists of digits.", "Error", JOptionPane.WARNING_MESSAGE);
			}
		}
		else
		{
			err = false;
			JOptionPane.showMessageDialog(null,"Enter PR number.", "Error", JOptionPane.WARNING_MESSAGE);
		}
		
		if(err){
			try{
				if(DepositController.findPR(prNumber.getText(), DateController.getDate())){
					key = Integer.parseInt(prNumber.getText());
				/*** ask for confirmation ***/
					
					if(DepositController.deleteDepositCheck(prNumber.getText(), DateController.getDate())){
						if(ActivitiesController.addActivity(DateController.getToday(), "Data deleted in deposit check table.")){
							this.depositView.populateTable();
							JOptionPane.showMessageDialog(null,"Data Deleted","Successful",JOptionPane.DEFAULT_OPTION);
						}
					}else JOptionPane.showMessageDialog(null,"An error happened.", "Error", JOptionPane.WARNING_MESSAGE);	
						
				}else{ 
					JOptionPane.showMessageDialog(null,"PR number does not exist.", "Error", JOptionPane.WARNING_MESSAGE);
				}
				prNumber.setText(null);
				closeframe();
			}catch (Exception a) { a.printStackTrace(); }
		}

	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		
		if ("delete".equals(e.getActionCommand())) {
			deleteData();
		}//end delete
	}
}
